package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.DestinationQuery;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Service
public class DestinationServiceImpl implements IDestinationService {


    @Autowired
    private DestinationRepository destinationRepository;

    @Autowired
    private RegionServiceImpl regionService;

    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    @Autowired
    private IStrategyService strategyService;



    @Autowired
    private MongoTemplate mongoTemplate;



    @Override
    public void save(Destination destination) {
        destination.setId(null);
        destinationRepository.save(destination);
    }

    @Override
    public void update(Destination destination) {
        destinationRepository.save(destination);
    }

    @Override
    public void delete(String id) {
        destinationRepository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return destinationRepository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return destinationRepository.findAll();
    }

    @Override
    public Page<Destination> query(DestinationQuery qo){
         //查询对象
        Query query = new Query();
        //带条件查询
        if(StringUtils.hasLength(qo.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(qo.getKeyword()));
        }

        if(StringUtils.hasLength(qo.getParentId())){
            query.addCriteria((Criteria.where("parentId").is(qo.getParentId())));
        }



        //查询总条数
        Long totalCount = mongoTemplate.count(query, Destination.class);

        //PageRequest.of方法设置分页
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<Destination> destinations = mongoTemplate.find(query, Destination.class);
        return new PageImpl(destinations,pageable,totalCount);
    }

    @Override
    public List<Destination> getDestByRegionId(String rid) {
        //先通过RegionId查询对应对象
        Region region = regionService.get(rid);
        //再通过对象获取对应的目的地id
        List<String> ids = region.getRefIds();
        return  destinationRepository.findByIdIn(ids);
    }

    @Override
    public List<Destination> queryToastsById(String parentId) {

        //创建一个集合，存放对应的数据
        List<Destination> children = new ArrayList<>();
      /*  if(!StringUtils.hasLength(parentId)){
            return null;
        }*/
       //通过while循环判断parentId是否有值
        while (StringUtils.hasLength(parentId)){
            //通过parentId查询目的地对象
            Destination dest = this.get(parentId);
            //将查询的目的地对象放入集合中
            children.add(dest);
            parentId = dest.getParentId();
        }
        Collections.reverse(children);

        return children;
    }

    @Override
    public List<Destination> queryDestByRegionId(String regionId) {
        //添加一个集合,用来获取对应国家下的省,在通过里面的省份对象添加对应的地市
        List<Destination> list = new ArrayList<>();
        if("-1".equals(regionId)){
            //如果页面点击的是-1,则查询出父id为中国的省份对象
            list = destinationRepository.findByParentName("中国");

        }else{
            //否则查询国外
            list = this.getDestByRegionId(regionId);
        }

        //遍历list集合,获取省份对象id,查询市对象
        for (Destination destination:list) {
            //通过省份id获取市对象后,加入省份集合中
            //PageRequest.of方法控制显示子目的地
            Pageable pageable = PageRequest.of(0,5, Sort.Direction.ASC,"_id");
            List<Destination> children = destinationRepository.findByParentId(pageable,destination.getId());
            destination.setChildren(children);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String id) {
        //通过目的地id查询攻略分类集合
        List<StrategyCatalog> CatalogList = strategyCatalogService.findByDestId(id);
        //然后通过循环遍历攻略分类集合，将查询到的攻略添加进对应的攻略分类中
        for (StrategyCatalog strategyCatalog:CatalogList){
          List<Strategy> StrategyList = strategyService.findByCatalogId(strategyCatalog.getId());
           strategyCatalog.setStrategies(StrategyList);
        }

        return CatalogList;

    }

    @Override
    public Destination queryByName(String name) {

        return destinationRepository.findByName(name);
    }


}
